export default {
  namespaced : true,
  state: {
    list: [
      { id: 1, name: '高考', done: false },
      { id: 2, name: '加油', done: false },
      { id: 3, name: '告白', done: true },
    ],
    type: 'all',
  },
  mutations: {
    changeDone(state,id) {
      const res = state.list.find(item => item.id === id)
      res.done = !res.done
    },
    del(state, id){
      state.list = state.list.filter(item => item.id !== id)
    },
    add( state,name) {
      state.list.unshift({
        id: Date.now(),
        name,
        done:false
      })
    },
    checkAll(state,value) {
      state.list.forEach(item => item.done = value)
    },
    edit(state,{id, name}){
      const todo = state.list.find((item) => item.id === id)
      todo.name = name
    },
    clear(state) {
      state.list = state.list.filter(item => !item.done)
    },
    changeType(state,type) {
      state.type = type
    }
  },
  actions: {

  },
  getters: {
    showList(state) {
      if (state.type === 'active') {
        return state.list.filter((item) => !item.done)
      } else if (state.type === 'completed') {
        return state.list.filter((item) => item.done)
      } else {
        return state.list
      }
    },
    isShow(state){
      return state.list.some(item => item.done)
    },
    leftCount(state) {
      return state.list.filter(item => !item.done).length
    }
  }
}